package com.hg.library.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hg.highglass_library.R;
import com.hg.library.Interface.SystemDevStateListener;
import com.hg.library.ui.imageview.BatteryView;

/**
 * Created by ASUS-PC on 2016/5/10.
 */
public class StatusBarView extends FrameLayout implements SystemDevStateListener {
    private View view;
    private ImageView bluetooth_img, headset_img, usd_img, wifi_img, airplane_img, lightning_img;
    private BatteryView battery_img;
    private TextView battery_txt, time_txt;
    private Context context;
    private int type;
    private int wifiLevel, percent;
    private boolean isLinked, isSDCardInsert, isUsbHandleInsert, isUsbStorageInsert, isHeadDevInsert, charging, full, isOn, isWifiEnable;
    private String time;

    public StatusBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StatusBarStyle);

        type = a.getInt(R.styleable.StatusBarStyle_type, 0);

        a.recycle();

        changeStyle(type);

        addView(view);

        initView();
    }

    public void setStyle(int type) {
        changeStyle(type);
        removeAllViewsInLayout();
        addView(view);
        initView();
        onAirplaneMode(isOn);
        onUsbHandleState(isUsbHandleInsert);
        onUsbStorageState(isUsbStorageInsert);
        onBatteryChargingState(charging);
        onBatteryFullState(full);
        onBatteryPercent(percent);
        onBluetoothState(isLinked);
        onHeadDevState(isHeadDevInsert);
        onSDCardState(isSDCardInsert);
        onTimeState(time);
        if (isWifiEnable) {
            onWifiEnable();
        } else {
            onWifiDisable();
        }
    }

    private void changeStyle(int type) {
        this.type = type;
        if (type == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.status_bar_view, null);
        } else if (type == 1) {
            view = LayoutInflater.from(context).inflate(R.layout.status_bar_view1, null);
        } else if (type == 2) {
            view = LayoutInflater.from(context).inflate(R.layout.status_bar_view2, null);
        }
    }

    private void initView() {
        bluetooth_img = (ImageView) view.findViewById(R.id.bluetooth_img);
        headset_img = (ImageView) view.findViewById(R.id.headset_img);
        usd_img = (ImageView) view.findViewById(R.id.usd_img);
        battery_img = (BatteryView) view.findViewById(R.id.battery_img);
        wifi_img = (ImageView) view.findViewById(R.id.wifi_img);
        battery_txt = (TextView) view.findViewById(R.id.battery_txt);
        time_txt = (TextView) view.findViewById(R.id.time_txt);
        airplane_img = (ImageView) view.findViewById(R.id.airplane_img);
        lightning_img = (ImageView) view.findViewById(R.id.lightning_img);
    }

    @Override
    public void onBluetoothState(boolean isLinked) {
        this.isLinked = isLinked;
        if (isLinked) bluetooth_img.setVisibility(View.VISIBLE);
        else bluetooth_img.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onSDCardState(boolean isInsert) {
        isSDCardInsert = isInsert;
    }

    @Override
    public void onUsbHandleState(boolean isInsert) {
        isUsbHandleInsert = isInsert;

        if (isInsert) {
            usd_img.setVisibility(View.VISIBLE);
        } else {
            usd_img.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onUsbStorageState(boolean isInsert) {
        isUsbStorageInsert = isInsert;

        if (isInsert) usd_img.setVisibility(View.VISIBLE);
        else usd_img.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onBatteryChargingState(boolean charging) {
        this.charging = charging;
        if (charging) {
            battery_img.setColor(Color.GREEN);
            lightning_img.setVisibility(View.VISIBLE);
        } else {
            if (battery_img.getBatteryPercent() > 20)
                battery_img.setColor(Color.WHITE);
            else
                battery_img.setColor(Color.RED);

            lightning_img.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBatteryFullState(boolean full) {
        this.full = full;
        if (full) battery_img.setColor(Color.WHITE);
        lightning_img.setVisibility(View.GONE);
    }

    private static final String TAG = "StatusBarView";

    @Override
    public void onBatteryPercent(int percent) {
        this.percent = percent;
        battery_txt.setText(percent + "%");
        battery_img.setBatteryPercent(percent);
        if (battery_img.getBatteryPercent() <= 20) {
            if (battery_img.getColor() != Color.GREEN)
                battery_img.setColor(Color.RED);
        }
    }

    @Override
    public void onTimeState(String time) {
        this.time = time;
        time_txt.setText(time);
    }

    @Override
    public void onWifiLevel(int Level) {
        wifiLevel = Level;
        if (Level > 0) {
            wifi_img.setVisibility(VISIBLE);
        } else {
            wifi_img.setVisibility(INVISIBLE);
            return;
        }

        int wifi_1 = R.drawable.wifi_1;
        int wifi_2 = R.drawable.wifi_2;
        int wifi_3 = R.drawable.wifi_3;
        if (type == 1) {
            wifi_1 = R.drawable.launcher_wifi_01;
            wifi_2 = R.drawable.launcher_wifi_02;
            wifi_3 = R.drawable.launcher_wifi_03;
        }

        switch (Level) {
            case 1:
            case 2:
                wifi_img.setImageResource(wifi_1);
                break;
            case 3:
            case 4:
                wifi_img.setImageResource(wifi_2);
                break;
            default:
                wifi_img.setImageResource(wifi_3);
                break;
        }
    }

    @Override
    public void onWifiEnable() {
        isWifiEnable = true;
        if (wifi_img.getVisibility() == View.INVISIBLE) wifi_img.setVisibility(View.VISIBLE);
    }

    @Override
    public void onWifiDisable() {
        isWifiEnable = false;
        if (wifi_img.getVisibility() == View.VISIBLE) wifi_img.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onAirplaneMode(boolean isOn) {
        this.isOn = isOn;
        if (isOn) airplane_img.setVisibility(VISIBLE);
        else airplane_img.setVisibility(INVISIBLE);
    }

    @Override
    public void onHeadDevState(boolean isInsert) {
        isHeadDevInsert = isInsert;
        if (isInsert) headset_img.setVisibility(View.VISIBLE);
        else headset_img.setVisibility(View.INVISIBLE);
    }

}
